package com.cayte.photo.scan;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

public class ImageActivity extends Activity {

	private View top, body, bottom;

	private ZoomImageView image;

	private String imagePath = null;
	private Bitmap bit = null;

	private GestureDetector gestureDetector;

	private int SCREEN_WIDTH, SCREEN_HEIGHT;

	private class ZoomSimpleGesture extends SimpleOnGestureListener {

		@Override
		public boolean onDown(MotionEvent e) {
			return super.onDown(e);
		}

		@Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
			return super.onSingleTapConfirmed(e);
		}

		@Override
		public boolean onDoubleTap(MotionEvent e) {
			image.toggleZoom(e);
			return true;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		SCREEN_WIDTH = dm.widthPixels;
		SCREEN_HEIGHT = dm.heightPixels;
		findView();
		initData();
	}

	private void findView() {
		image = (ZoomImageView) findViewById(R.id.zoomImageView);
		top = findViewById(R.id.top);
		body = findViewById(R.id.body);
		bottom = findViewById(R.id.bottom);
	}

	private void initData() {
		imagePath = Environment.getExternalStorageDirectory().getPath()
				+ "/DCIM/Screenshots/b.jpg";
		bit = loadImageFromFile(imagePath, SCREEN_WIDTH, SCREEN_HEIGHT);

		initView();

		image.setCutSize(hBody);
		image.setBitmap(bit);

		gestureDetector = new GestureDetector(this, new ZoomSimpleGesture());
	}

	private Bitmap loadImageFromFile(String path, int width, int height) {
		Bitmap bitmap = null;
		try {
			BitmapFactory.Options opts = new BitmapFactory.Options();
			opts.inJustDecodeBounds = true;
			bitmap = BitmapFactory.decodeFile(path, opts);
			opts.inJustDecodeBounds = false;
			int be_w = (int) (opts.outWidth / (float) width);
			int be_h = (int) (opts.outHeight / (float) height);
			int be = Math.max(be_w, be_h);
			be--;
			if (be <= 0)
				be = 1;
			opts.inSampleSize = be;
			bitmap = BitmapFactory.decodeFile(path, opts);
		} catch (Exception e) {
		}
		return bitmap;
	}

	private int hTop, hBody, hBottom;

	private void initView() {
		hBody = SCREEN_WIDTH;
		// hBottom = (SystemParams.SCREEN_HEIGHT
		// - DisplayUtil.dip2px(getResources().getDimension(
		// R.dimen.actionbar_height)) - hBody) / 2;
		hBottom = (SCREEN_HEIGHT - hBody) / 2;
		hTop = SCREEN_HEIGHT - hBody - hBottom;
		LayoutParams lp = null;
		//
		lp = top.getLayoutParams();
		lp.height = hTop;
		top.setLayoutParams(lp);
		lp = null;
		//
		lp = body.getLayoutParams();
		lp.height = hBody;
		body.setLayoutParams(lp);
		lp = null;
		//
		lp = bottom.getLayoutParams();
		lp.height = hBottom;
		bottom.setLayoutParams(lp);
		lp = null;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		gestureDetector.onTouchEvent(event);
		return image.touchEvent(event);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		clearBitmap();
	}

	private void clearBitmap() {
		image.setBitmap(null);
		if (bit != null && !bit.isRecycled())
			bit.recycle();
		bit = null;
	}

}
